package com.jl.commons.aop;

import com.jl.commons.constant.Constants;
import com.jl.model.in.LoginUser;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 校验用户是否登录进入系统
 */
@Aspect
@Component
public class CheckLogin {
    /**
     * 指定切点
     * 匹配 com.jl.controller包及其子包下的所有类的所有方法
     */
    @Pointcut("execution(public * com.jl.controller.*.*(..)) && !execution(public * com.jl.controller.LoginController.*(..))")
    public void checkLogin(){
    }

    @Before("checkLogin()")
    public void doBefore(JoinPoint joinPoint){
        // 接收到请求，记录请求内容
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest req = attributes.getRequest();
//        LoginUser loginUser = (LoginUser) req.getSession().getAttribute(Constants.CURRENT_USER);
//        HttpServletResponse response = attributes.getResponse();
//        if(loginUser == null){
//            try {
//                //如果不存在登录信息则重定向页面
//                response.sendRedirect("/index");
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
    }



}
